package com.person.leetcode.editor.cn;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

/**
 * 104.二叉树的最大深度
 * 2022-08-10 10:47:21
 **/
public class Sequence_0104_MaximumDepthOfBinaryTree {
    public static void main(String[] args) {
        Solution solution = new Sequence_0104_MaximumDepthOfBinaryTree().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     * int val;
     * TreeNode left;
     * TreeNode right;
     * TreeNode() {}
     * TreeNode(int val) { this.val = val; }
     * TreeNode(int val, TreeNode left, TreeNode right) {
     * this.val = val;
     * this.left = left;
     * this.right = right;
     * }
     * }
     */
    class Solution {
        public int maxDepth(TreeNode root) {
            Queue<TreeNode> queue = new LinkedList<>();
            int ans = 0;
            if (root==null) return 0;
            queue.offer(root);
            while (!queue.isEmpty()){
                int size = queue.size();//当前层下所有节点,需要出队的次数
                while (size>0){
                    TreeNode node = queue.poll();
                    if (node.left!=null)
                    queue.offer(node.left);
                    if (node.right!=null)
                    queue.offer(node.right);
                    size--;
                }//完成层级替换
                ans ++;
            }
            return ans;
        }
    }

//    class Solution {
//    public int maxDepth(TreeNode root) {
//        if (root == null) {
//            return 0;
//        } else {
//            int leftHeight = maxDepth(root.left);
//            int rightHeight = maxDepth(root.right);
//            return Math.max(leftHeight, rightHeight) + 1;
//        }
//    }

//        public int maxDepth(TreeNode root) {
//            if (root == null) {
//                return 0;
//            }
//            Queue<TreeNode> queue = new LinkedList<TreeNode>();
//            queue.offer(root);
//            int ans = 0;
//            while (!queue.isEmpty()) {
//                int size = queue.size();//父层
//                while (size > 0) {//遍历父层,获取子层
//                    TreeNode node = queue.poll();
//                    if (node.left != null) {
//                        queue.offer(node.left);
//                    }
//                    if (node.right != null) {
//                        queue.offer(node.right);
//                    }
//                    size--;
//                }
//                ans++;
//            }
//            return ans;
//        }
//    }


//leetcode submit region end(Prohibit modification and deletion)

}